# 编译器和编译选项
CXX = g++
CXXFLAGS = -std=c++14 -Wall -O2 -MMD -MP -I./ConfigManager/libs

# SQLite3 库链接选项
LIBS = -lsqlite3

# 项目目标
TARGET = smartHomeApp

# 子目录列表
SUBDIRS = ConfigManager \
          DatabaseManager \
          DeviceManager \
          ExpectionManager \
          LogManager \
          MultiThreadManager \
          UserManager \
          MenuManager \
          Utils

# 源文件和目标文件
SRCS = $(wildcard main.cpp) \
       $(foreach dir, $(SUBDIRS), $(wildcard $(dir)/*.cpp))

BUILD_DIR = build
OBJS = $(addprefix $(BUILD_DIR)/obj/, $(SRCS:.cpp=.o))
DEPS = $(OBJS:.o=.d)

# 包含头文件路径
INCLUDES = $(foreach dir, $(SUBDIRS), -I$(dir))

# 目标规则
all: $(TARGET)

# 创建 build 目录
$(BUILD_DIR):
	@mkdir -p $(BUILD_DIR)/obj

# 链接生成可执行文件
$(TARGET): $(BUILD_DIR) $(OBJS)
	$(CXX) $(CXXFLAGS) $(OBJS) $(LIBS) -o $@

# 编译规则，生成 .o 和 .d 文件
$(BUILD_DIR)/obj/%.o: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@

# 依赖规则，生成 .d 文件
$(BUILD_DIR)/obj/%.d: %.cpp
	@mkdir -p $(dir $@)
	$(CXX) $(CXXFLAGS) $(INCLUDES) -M $< -MF $@

# 包含自动生成的依赖文件
-include $(DEPS)

# 清理规则
clean:
	rm -rf $(BUILD_DIR) $(TARGET)

# 伪目标
.PHONY: all clean
